﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gurucore.DTOGenerator
{
	public partial class CodeViewer : UserControl
	{
		public CodeViewer()
		{
			InitializeComponent();
		}

		private string m_sCode;

		public string Code
		{
			get { return m_sCode; }
			set 
			{ 
				m_sCode = value;
				wbrViewer.DocumentText = this.FormatCodeAsHTML(m_sCode);
			}
		}

		private string FormatCodeAsHTML(string p_sCode)
		{
			List<string> arrKeyWords = new List<string>(new string[] 
			{
				"using",
				"namespace",
				"xclass",
				"public",
				"private",
				"virtual",
				"const",
				"true",
				"false",
				"bool",
				"byte",
				"short",
				"ushort",
				"int",
				"uint",
				"long",
				"ulong",
				"decimal",
				"string"
			});

			List<string> arrKnownWords = new List<string>(new string[] 
			{
				"MappedTable",
				"MappedColumn",
				"DateTime"
			});

			StringBuilder oHtmlBuilder = new StringBuilder();
			oHtmlBuilder.Append("<html>");
			oHtmlBuilder.Append("<head>");
			oHtmlBuilder.Append("<style type=\"text/css\">");
			oHtmlBuilder.Append("<!--");
			oHtmlBuilder.Append(".clsCode {");
			oHtmlBuilder.Append("	font-family: \"Courier New\", Courier, monospace;");
			oHtmlBuilder.Append("	font-size: 9pt;");
			oHtmlBuilder.Append("}");
			oHtmlBuilder.Append(".clsKeyWord {");
			oHtmlBuilder.Append("	color: #0000FF;");
			oHtmlBuilder.Append("}");
			oHtmlBuilder.Append(".clsKnownWord {");
			oHtmlBuilder.Append("	color: #009999;");
			oHtmlBuilder.Append("}");
			oHtmlBuilder.Append(".clsStringConstant {");
			oHtmlBuilder.Append("	color: #660000;");
			oHtmlBuilder.Append("}");
			oHtmlBuilder.Append("-->");
			oHtmlBuilder.Append("</style>");
			oHtmlBuilder.Append("</head>");
			oHtmlBuilder.Append("<body class=\"clsCode\">");

			StringBuilder oCodeBuilder = new StringBuilder(p_sCode);
			oCodeBuilder.Replace(" ", "&nbsp;").Replace("\"", "&quot;").Replace("\n", "<br />").Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
			oCodeBuilder.Replace("class", "xclass");

			bool bFlag = false;
			int nCursor = 0;
			while (nCursor < oCodeBuilder.Length)
			{
				if ((oCodeBuilder[nCursor] == '&') &&
					(oCodeBuilder[nCursor + 1] == 'q') &&
					(oCodeBuilder[nCursor + 2] == 'u') &&
					(oCodeBuilder[nCursor + 3] == 'o') &&
					(oCodeBuilder[nCursor + 4] == 't') &&
					(oCodeBuilder[nCursor + 5] == ';'))
				{
					bFlag = !bFlag;
					if (bFlag)
					{
						oCodeBuilder.Insert(nCursor, "<span class=\"clsStringConstant\">");
						nCursor += 33;
					}
					else
					{
						oCodeBuilder.Insert(nCursor + 6, "</span>");
						nCursor += 9;
					}
				}
				else
				{
					nCursor++;
				}
			}

			foreach (string sKeyWord in arrKeyWords)
			{
				oCodeBuilder.Replace(sKeyWord, "<span class=\"clsKeyWord\">" + sKeyWord + "</span>");
			}

			foreach (string sKnownWord in arrKnownWords)
			{
				oCodeBuilder.Replace(sKnownWord, "<span class=\"clsKnownWord\">" + sKnownWord + "</span>");
			}

			oCodeBuilder.Replace("xclass", "class");

			oHtmlBuilder.Append(oCodeBuilder.ToString());
			
			oHtmlBuilder.Append("</body></html>");

			return oHtmlBuilder.ToString();
		}
	}
}
